package com.party.admin.web.controller.merchantAnalyze;

import com.google.common.collect.Maps;
import com.party.admin.biz.merchantAnalyze.MemberMerchantCountBizService;
import com.party.admin.biz.merchantAnalyze.MemberMerchantDetailBizService;
import com.party.admin.biz.merchantAnalyze.MerchantAnalyzeBizService;
import com.party.admin.web.dto.AjaxResult;
import com.party.admin.web.dto.output.merchantAnalyze.MerchantAnalyzeListOutput;
import com.party.admin.web.dto.output.merchantAnalyze.MerchantAnalyzeOutput;
import com.party.common.paging.Page;
import com.party.common.utils.DateUtils;
import com.party.core.model.member.*;
import com.party.core.service.member.IMemberActService;
import com.party.core.service.member.IMemberGroupService;
import com.party.core.service.member.IMerchantAnalyzeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 机构数据分析控制器
 * Created by wei.li
 *
 * @date 2017/9/13 0013
 * @time 9:24
 */

@Controller
@RequestMapping(value = "merchant/analyze")
public class MerchantAnalyzeController {

    @Autowired
    private MerchantAnalyzeBizService merchantAnalyzeBizService;

    @Autowired
    private IMemberGroupService memberGroupService;

    @Autowired
    private IMerchantAnalyzeService merchantAnalyzeService;

    @Autowired
    private IMemberActService memberActService;

    @Autowired
    private MemberMerchantCountBizService memberMerchantCountBizService;

    @Autowired
    private MemberMerchantDetailBizService memberMerchantDetailBizService;

    protected static Logger logger = LoggerFactory.getLogger(MerchantAnalyzeController.class);
    /**
     * 初始化数据
     * @return
     */

    @ResponseBody
    @RequestMapping(value = "init")
    public AjaxResult init(){
        try {
            merchantAnalyzeBizService.init();
        } catch (Exception e) {
            logger.error("初始化异常", e);
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }


    /**
     * 初始化统计总计
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "countInit")
    public AjaxResult countInit(){
        memberMerchantCountBizService.init();
        return AjaxResult.success();
    }

    /**
     * 初始化统计详情
     * @return 详情
     */
    @ResponseBody
    @RequestMapping(value = "detailInit")
    public AjaxResult detailInit(){
        memberMerchantDetailBizService.init();
        return AjaxResult.success();
    }

    @ResponseBody
    @RequestMapping(value = "revise")
    public AjaxResult revise(){
        List<MerchantAnalyze> list = merchantAnalyzeService.list(new MerchantAnalyze());
        for (MerchantAnalyze merchantAnalyze : list){
            Integer appNum =  memberActService.count(merchantAnalyze.getMemberId(),
                    DateUtils.formatDate(merchantAnalyze.getCreateDate()),
                    DateUtils.formatDate(merchantAnalyze.getCreateDate()), false);
            merchantAnalyze.setApplyNum(appNum);
            merchantAnalyzeService.update(merchantAnalyze);
        }
        return AjaxResult.success();
    }
    /**
     * 列表
     * @param memberMerchantDetailQuery 机构分析
     * @param page 分页参数
     * @return 列表数据
     */
    @RequestMapping(value = "list")
    public ModelAndView list(MemberMerchantDetailQuery memberMerchantDetailQuery, Page page){
        page.setLimit(20);
        ModelAndView modelAndView = new ModelAndView("merchantAnalyze/merchantAnalyzeList");
        List<MerchantAnalyzeListOutput> list = memberMerchantDetailBizService.list(memberMerchantDetailQuery, page);
        modelAndView.addObject("list", list);
        modelAndView.addObject("page", page);
        return modelAndView;
    }


    /**
     * 机构统计详情
     * @param memberId 会员编号
     * @return 统计数据
     */
    @RequestMapping(value = "detail")
    public ModelAndView detail(String memberId){
        ModelAndView modelAndView = new ModelAndView("merchantAnalyze/merchantAnalyzeDetail");
        //Map<String, MerchantAnalyze> map = merchantAnalyzeBizService.detail(memberId);
        HashMap<String, MerchantAnalyzeOutput> map = memberMerchantDetailBizService.detail(memberId);
        MemberGroup member = memberGroupService.get(memberId);
        modelAndView.addObject("name",member.getRealname());
        modelAndView.addObject("map", map);
        modelAndView.addObject("memberId", memberId);
        return modelAndView;
    }


    /**
     * 图表
     * @param type 类型
     * @param length 长度
     * @param memberId 会员编号
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "chart")
    public AjaxResult chart(Integer type, Integer length, String memberId){
        AjaxResult ajaxResult = new AjaxResult();
        length = null == length ? 30 : length;
        type = null == type ? 3 : type;
        List<String> dateList = merchantAnalyzeBizService.dateStringList(length);
        List<String> data = memberMerchantDetailBizService.oneChartList(length, memberId, type);
        HashMap<String, List> map = Maps.newHashMap();
        map.put("dateList", dateList);
        map.put("dataList", data);
        ajaxResult.setData(map);
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    /**
     * admin专用图表
     * @param type 类型
     * @param length 长度
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "adminchart")
    public AjaxResult adminChart(Integer type, Integer length){
        AjaxResult ajaxResult = new AjaxResult();
        length = null == length ? 30 : length;
        type = null == type ? 3 : type;
        List<String> dateList = merchantAnalyzeBizService.dateStringList(length);
        List<String> data = memberMerchantDetailBizService.allChaetList(length, type);
        HashMap<String, List> map = Maps.newHashMap();
        map.put("dateList", dateList);
        map.put("dataList", data);
        ajaxResult.setData(map);
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }
}
